package givenchy
import org.codehaus.groovy.grails.plugins.springsecurity.Secured
@Secured(['ROLE_USER','ROLE_ADMIN'])
class TaskController {
    def authenticateService
    def vipLimitdService
    static allowedMethods = [save: "POST", update: "POST"]
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        //println grailsApplication.config.grails.vip.great
        def taskGroup = TaskGroup.get( params.long("id"))
        checkAuthor(taskGroup)
        if(taskGroup){ 
            params.max = Math.min(params.max ? params.int('max') : 10, 100)
            [taskInstanceList: Task.findAllByTaskGroup(taskGroup ,params),
                taskInstanceTotal: Task.countByTaskGroup(taskGroup),
                    id:params.id ]
        }else{
            flash.message = "没找到任务列表"
            redirect(controller:"taskGroup",action: "list")
        }
    }

    //检查任务组的ID和登陆的用户是否匹配，就是说这个组我是不是有权限访问。
    def checkAuthor(taskGroupInstance){
        if(taskGroupInstance){
            if(taskGroupInstance && taskGroupInstance.person.id != authenticateService.userDomain().id){
                 flash.message = "您没有权限访问！！！！！"
                 redirect(redirect(uri:"/autherror"))
                //return false;
            }
        }else{
            flash.message = "您没有权限访问！！！！！"
            redirect(redirect(uri:"/autherror"))
        }
    }

    // 检查Task是不是自己的
    def checkTask(taskInstance){
        if(taskInstance && taskInstance.person.id != authenticateService.userDomain().id){
             flash.message = "您没有权限访问！！！！！"
             redirect(redirect(uri:"/autherror"))
            //return false;
        }
    }

    // 普通任务
    def create = {
        TaskGroup tg = TaskGroup.get( params.long("id"))
	    checkAuthor(tg)
        def taskInstance = new Task()
        taskInstance.properties = params
        taskInstance.taskGroup = tg
        return [taskInstance: taskInstance]
    }
/**
    // 高级任务
    def createAd = {
	    checkAuthor(params.id)
        def person = authenticateService.userDomain()
        if(person.vip > 0){
            def taskInstance = new Task()
            taskInstance.properties = params
            return [taskInstance: taskInstance]
        }else{
            flash.message = "您不是Vip用户，不能添加高级任务"
            return redirect(action: "list")
        }
    }
*/
    def save = {
        def taskInstance = new Task(params)
        // 根据用户等级，检查录入的actions是否符合要求
        def ms = vipLimitdService.checkTaskActions(taskInstance.actions,grailsApplication)
        if(ms){
            flash.message = ms
            return render(view: "create", model: [taskInstance: taskInstance ])
        }
        checkAuthor(taskInstance.taskGroup)
        taskInstance.person = authenticateService.userDomain()
        if (taskInstance.save(flush: true)) {
            flash.message = "保存成功${taskInstance.name}"
            redirect(action: "list" , id : taskInstance.taskGroup.id)
        }
        else {
            render(view: "create", model: [taskInstance: taskInstance ])
        }
    }
/**
    def saveAd = {
        def taskInstance = new Task(params)
        if(taskInstance.taskGroup.person.id != authenticateService.userDomain().id){
            flash.message = "您没有权限访问！！！！！"
            return redirect(redirect(uri:"/autherror"))
        }
        taskInstance.person = authenticateService.userDomain()
        if(taskInstance.person.vip < 1){
            taskInstance.isAd = false;
            taskInstance.requireOfIP = 0;
        }else{
            taskInstance.isAd = true;
        }
      
        if (taskInstance.save(flush: true)) {
            
            flash.message = "保存成功${taskInstance.name}"
            redirect(action: "list" , id : taskInstance.taskGroup.id)
        }
        else {
            render(view: "createAd", model: [taskInstance: taskInstance])
        }
    }
    */
    /** 用不到
    def show = {
        def taskInstance = Task.get(params.id)
        if (!taskInstance) {
            flash.message = "任务不存在"
            redirect(action: "list")
        }
        else {
            [taskInstance: taskInstance]
        }
    }
    */

    // 普通任务编辑
    def edit = {
        def taskInstance = Task.get(params.id)
        if (!taskInstance) {
            flash.message = "没有找到此任务"
            redirect(action: "list")
        }
        else {
            checkTask(taskInstance)
            return [taskInstance: taskInstance]
        }
    }
    

    def update = {
        def taskInstance = Task.get(params.id)
        def ms = vipLimitdService.checkTaskActions(taskInstance.actions,grailsApplication)
        if(ms){
            flash.message = ms
            return render(view: "edit", model: [taskInstance: taskInstance ])
        }
        checkTask(taskInstance)
        if (taskInstance) {
            taskInstance.properties = params
      
            if (!taskInstance.hasErrors() && taskInstance.save(flush: true)) {
                flash.message = "任务修改成功"
                redirect(action: "list" , id : taskInstance.taskGroup.id)
            }
            else {
                render(view: "edit", model: [taskInstance: taskInstance])
            }
        }
        else {
            flash.message = "没有找到任务"
            redirect(action: "list" , id : taskInstance.taskGroup.id)
        }
    }

    def delete = {
        def taskInstance = Task.get(params.id)
        if (taskInstance) {
            try {
                checkTask(taskInstance)
                taskInstance.delete(flush: true)
                flash.message = "删除成功"
                redirect(action: "list" , id : params.pid)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "删除失败"
                redirect(action: "list" , id : params.pid)
            }
        }
        else {
            flash.message = "没有找到任务}"
            redirect(action: "list" , id : params.pid)
        }
    }


   def close = {
        Task task = Task.get(params.id)
        if (task) {
            task.available = false
            task.save();
        }
        redirect(action: "list" , id : task.taskGroup.id)
    }

    def open = {
        Task task = Task.get(params.id)
        if (task) {
            task.available = true
            task.save();
        }
        redirect(action: "list" , id : task.taskGroup.id)
    }
}
